package org.management.system.controller;


import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.management.base.BaseController;
import org.management.base.data.PermissionCode;
import org.management.base.data.Result;
import org.management.system.entity.ResourceEntity;
import org.management.system.service.ResourceService;
import org.management.system.service.RoleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 资源控制类
 * @author jiangyan
 *
 */
@RestController
@Api("资源权限模块")
@RequestMapping(value = "/resources")
public class ResourceController extends BaseController<ResourceEntity> {

	@Resource
	private ResourceService resourceService;

	@Resource
	private RoleService roleService;

	/**
	 *
	 * 添加数据获取父级id 如果为不为空并且为叶子节点 修改isLeaf 为false
	 * 否则将当前节点作为最顶级（0）同时修改当前节点为叶子节点
	 * 如果id不为空默认是需要递归修改子节点的层级
	 */
	@PostMapping
	@ApiOperation("保存资源信息")
	@ApiImplicitParam(name = "entity",value = "用户登录密码",dataType = "ResourceEntity",required = true)
	@RequiresPermissions(value = PermissionCode.USER_WRITE_EDIT)
	@Override
	public Result saveEntity(@Validated ResourceEntity entity) {
		//节点原有 父级id存在 且出了节点之外不存在其他节点 修改父节点状态为叶子节点
			resourceService.saveWithUpdateStatus(entity);
		return Result.success("保存资源信息成功");
	}

	/**
	 * 先根据当前节点id删除节点
	 * 同时判断父节点是否还存在子节点，不存在修改IsLeaf 为true 叶子节点
	 * 同时递归删除当前节点下的子节点
	 */
	@Override
	@DeleteMapping(value = "/{id}")
	@ApiOperation("删除资源信息")
	@ApiImplicitParam(name = "id",value = "资源ID",dataType = "String",required = true)
	@RequiresPermissions(value = PermissionCode.USER_WRITE_DEL)
	public Result delete(@PathVariable @NotEmpty(message = "资源ID不能为空") String id) {
		this.resourceService.deleteWithChildrenById(id);
		return Result.success("删除成功");
	}

	/**
	 * 获取除本身资源节点及子节点数据
	 */
	@GetMapping(value="/{id}/children")
    @ApiOperation("根据资源ID获取子节点资源信息")
    @ApiImplicitParam(name = "id",value = "资源ID",dataType = "String",required = true)
	@RequiresPermissions(value = PermissionCode.USER_READ_LIST)
	public Result getExcludeNodes(@PathVariable @NotEmpty(message = "资源ID不能为空") String id){
		return Result.success("获取子资源信息成功",this.resourceService.getExcludeResources(id));
	}

	/**
	 * 根据资源id获取角色集合
	 */
	@GetMapping(value="/{id}/roles")
	@ApiOperation("根据资源id获取角色集合 ")
	@ApiImplicitParam(name = "id",value = "资源ID",dataType = "String",required = true)
	@RequiresPermissions(value = PermissionCode.USER_READ_LIST)
	public Result getRolesByResourceId(@PathVariable @NotEmpty(message = "资源ID不能为空") String id){
		return Result.success("获取信息成功", this.roleService.getRolesByResourceId(id));
	}

	/**
	 * 根据资源id获取角色id字符串
	 */
	@GetMapping(value="{id}/roleIds")
	@ApiOperation("根据资源id获取角色id字符串 ")
	@ApiImplicitParam(name = "id",value = "资源ID",dataType = "String")
	@RequiresPermissions(value = PermissionCode.USER_READ_LIST)
	public Result getRoleIdsByResourceId(@PathVariable @NotEmpty(message = "资源ID不能为空") String id){
		return Result.success("获取信息成功", this.roleService.getRoleIdsByResourceId(id));
	}

	/**
	 * 资源id关联角色集合
	 * @param id 资源ID
	 * @param roleIds 角色字符串
	 * @return Result
	 */
	@PostMapping(value="/{id}/roles")
	@ApiOperation("资源id关联角色集合")
	@ApiImplicitParams({
			@ApiImplicitParam(name="id",value="资源ID",dataType="String"),
			@ApiImplicitParam(name="roleIds",value="多个角色ID",dataType="String")
	})
	@RequiresPermissions(value = PermissionCode.USER_WRITE_EDIT)
	public Result saveRoleRelation(@PathVariable @NotEmpty(message = "资源ID不能为空") String id,
	                               @NotEmpty(message = "角色集合不能为空") String roleIds) {
		String [] roleArray = roleIds.split(",");
		this.roleService.saveRoleRelation(id, roleArray);
		return Result.success("保存成功");
	}
}
